<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>字符串</title>
</head>
<body>
    <!-- 转换为自负串 -->
    <script>
        // 隐式类型转换
        console.log('隐式类型转换');
        var str = '' + 2;
        console.log("typeof('' + 2) = " + typeof str);
        str = '' + null;
        console.log("typeof('' + null) = " + typeof str);
        str = '' + undefined;
        console.log("typeof('' + undefined) = " + typeof str);
        str = '' + true;
        console.log("typeof('' + true) = " + typeof str);

        // 显示类型转换
        // String()
        str = String(2);
        console.log('typeof(String(2)) = ' + typeof str);
        str = String(null);
        console.log('typeof(String(null) = ' + typeof str);
        str = String(undefined);
        console.log('typeof(undefined) = ' + typeof str);
        str = String(true);
        console.log('typeof(true) = ' + typeof str);
        // toString()
        var iNum = 80;
        str = iNum.toString();
        console.log('80.toString() = ' + typeof str);
        str = iNum.toString(16);
        console.log('80.toString(16) = ' + typeof str);
        str = true.toString();
        console.log('true.toString() = ' + typeof str);
        // str = null.toString(); // 不可以
        // console.log('null.toString() = ' + typeof str);
        // str = undefined.toString(); // 不可以
        // console.log('undefined.toString() = ' + typeof str);

        // 二进制的字符串，怎么变成16进制的字符串
        str = '10101001';
        var nNum = parseInt(str, 2);
        if(!isNaN(nNum)) {
            console.log(nNum.toString(16));
        }
    </script>
</body>
</html>